<?php
    require_once'config.php';
    require_once'weixin_info.php';
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $data_num = $_POST['data_num'];
        $order_sql = $mysql->query("select * from dyspw_goods_order where ordernum = {$data_num}");
        $order = $order_sql->fetch_all(MYSQLI_ASSOC);
        $shop_id = $order[0]['shop_id'];
        $goods_sql = $mysql->query("select * from dyspw_goods where id = {$order[0]['goods_id']}");
        $goods_info = $goods_sql->fetch_all(MYSQLI_ASSOC);
        $shop_sql = $mysql->query("select * from dyspw_member where id =" . $shop_id);
        $shop_info = $shop_sql->fetch_all(MYSQLI_ASSOC);
        if(empty($data_num) || empty($order) || !is_array($order)){
            $result['code'] = 'N';
            $result['code'] = '未查询到该订单信息或订单为空';
            exit(json_encode($result));
        }
        if($order[0]['shop_id'] == $_SESSION['Member_info'][0]['id']){
            if($order[0]['state'] == 4){
                $result['code'] = 'N';
                $result['code'] = '该订单已过期';
                exit(json_encode($result));
            }elseif ($order[0]['state'] == 3) {
                $result['code'] = 'N';
                $result['code'] = '订单已核销，无法二次核销';
                exit(json_encode($result));
            }elseif ($order[0]['state'] == 1 && empty($order[0]['uptime'])) {

                $Up1 = "Update dyspw_goods_order set state = 3, uptime = '".time()."' where id = ".$order[0]['id'];
                $update1 = mysqli_query($mysql, $Up1);
                if (!$update1) {
                    $result['code'] = 'N';
                    $result['code'] = '更新订单信息失败';
                    exit(json_encode($result));
                }
                //核销成功后添加商户收支明细，给当前商户余额增值
                $income_money = $order[0]['money'] - ($order[0]['money'] * 0.03);
                $time = time();

                $mysql->query("INSERT INTO dyspw_shop_detailed (u_id,o_id,goods_id,goods_name,o_money,goods_img,u_name,u_avatar,income_money, hexiao_time,type) VALUES ('{$shop_id}', '{$order[0]['id']}', '{$order[0]['goods_id']}', '{$goods_info[0]['title']}', '{$order[0]['money']}', '{$goods_info[0]['img']}', '{$shop_info[0]['username']}', '{$shop_info[0]['avatar']}', '{$income_money}',{$time}, '1')");
                $mysql->query("Update dyspw_member set balance = balance + {$income_money} where id = {$shop_id}");


                if($order[0]['guide_uid'] != 0 && !empty($order[0]['guide_uid'])){
                    exit('guide');
                    $guide_sql = $mysql->query("select * from dyspw_shop_guide where guide_id = {$order[0]['guide_uid']}");
                    $guide = $guide_sql->fetch_all(MYSQLI_ASSOC);
                    if(!empty($guide)){
                        $mysql->query("Update dyspw_shop_guide set guide_money = guide_money + {$order[0]['money']} where guide_id = {$order[0]['guide_uid']}");
                    }
                }
                $result['code'] = 'Y';
                $result['code'] = '该订单发货成功，即将刷新';
                exit(json_encode($result));

            }else{
                $result['code'] = 'N';
                $result['code'] = '该订单状态不允许收货';
                exit(json_encode($result));
            }

        }else{
            $result['code'] = 'N';
            $result['code'] = '商户信息不匹配';
            exit(json_encode($result));
        }
    }







 ?>
